<template>
    <div class="content">
        <div class="clearfix">
            <Form class="f-fl" inline :label-width="5">
                <FormItem>
                    <Input
                        v-model="params.userName"
                        placeholder="教师账号/教师姓名"
                    ></Input>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="search">搜索</Button>
                </FormItem>
                <FormItem>
                    <Button @click="clearSearch">清空</Button>
                </FormItem>
            </Form>
            <div class="f-fr">
                <Button type="primary" @click="handleAdd">新建教师</Button>
            </div>
        </div>
        <Table :data="list" :columns="columns" border align="center">
            <template slot-scope="{ row }" slot="userName">
                <div>{{ row.userName }}</div>
            </template>
            <template slot-scope="{ row }" slot="realName">
                <div>{{ row.realName }}</div>
            </template>
            <template slot-scope="{ row }" slot="contacts">
                <div>{{ row.contacts }}</div>
            </template>
            <template slot-scope="{ row }" slot="createTime">
                <div>{{ row.createTime }}</div>
            </template>
            <template slot-scope="{ row }" slot="action">
                <div class="table-action">
                    <Button type="text" @click="handleEdit(row.userId)">编辑</Button>
                    <Button type="text" @click="handleDelete(row.userId)">删除</Button>
                    <Button
                        type="text"
                        @click="handleRestPwd(row.userId)"
                    >重置密码</Button>
                </div>
            </template>
        </Table>
        <Pagination
            :pageSize.sync="params.pageSize"
            :current.sync="params.pageNum"
            :total="params.total"
            @change="queryList"
        />
    </div>
</template>
<script>
import { queryTeacherList, delTeacher } from '@/api/userManage'
import { resetPwd } from '@/api/user'

export default {
    inject: ['nextPageReload'],
    data () {
        return {
            params: {
                total: 0,
                pageNum: 1,
                pageSize: 10,
                userName: ''
            },
            list: [],
            columns: Object.freeze([
                { title: '序号', type: 'index', align: 'center', width: 80 },
                { title: '教师账号', slot: 'userName', align: 'center' },
                { title: '教师姓名', slot: 'realName', align: 'center' },
                { title: '学院名称', key: 'collegeName', align: 'center' },
                { title: '专业名称', key: 'majorName', align: 'center' },
                { title: '联系方式', slot: 'contacts', align: 'center' },
                { title: '创建时间', slot: 'createTime', align: 'center' },
                { title: '操作', slot: 'action', align: 'center', width: 140 }
            ])
        }
    },
    mounted () {
        this.queryList()
    },
    methods: {
        search () {
            this.params.pageNum = 1
            this.queryList()
        },
        queryList () {
            queryTeacherList(this.params).then(res => {
                if (res.data.code === '0000') {
                    const data = res.data.data
                    this.list = data.list
                    this.params.total = data.total
                }
            })
        },
        clearSearch () {
            this.params.userName = ''
            this.params.pageNum = 1
            this.queryList()
        },
        handleAdd () {
            this.nextPageReload()
            this.$router.push({
                name: 'teaManageAdd'
            })
        },
        handleEdit (id) {
            this.nextPageReload()
            this.$router.push({
                name: 'teaManageEdit',
                params: {
                    id
                }
            })
        },
        handleDelete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '确认是否删除？',
                onOk: () => {
                    delTeacher(id).then(res => {
                        if (res.data.code === '0000') {
                            this.$Message.success('删除成功')
                            this.clearSearch()
                        } else {
                            this.$Message.error(res.data.msg)
                        }
                    })
                }
            })
        },
        handleRestPwd (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '确认是否重置密码？',
                onOk: () => {
                    resetPwd(id).then(res => {
                        if (res.data.code === '0000') {
                            this.$Message.success({
                                content: `重置成功，新密码：${res.data.msg}`,
                                duration: 4
                            })
                        } else {
                            this.$Message.error(res.data.msg)
                        }
                    })
                }
            })
        }
    }
}
</script>
